package team.cw.land.test.activiti;

import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import team.cw.land.test.activiti.dto.ActivitiDTO;
import team.cw.land.test.activiti.service.IActivitiService;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class ActivitiApplicationTests2 {

    @Resource
    private IActivitiService activitiService;

    private static Map<String,Object> map = new HashMap<>();
    static {
        map.put("user","国土张三");
        map.put("user1","国土张三");
        map.put("user2","国土张三");
        map.put("user3","国土大队长");
        map.put("user4","国土副支队长");
        map.put("user5","国土支队长");
        map.put("user6","监察李四");
        map.put("user7","监察负责人");
        map.put("user8","分管领导");
        map.put("user9","分管王五");
    }

    //最短一条线
    @Test
    public void test() throws Exception {
        //最短一条线
        String userName = "国土张三";
        String processId = activitiService.startProcess("案件流程", map);
        getProcessImg(processId);
        sp(userName,"0",processId);
        getProcessImg(processId);
        sp(userName,"2",processId);
        getProcessImg(processId);
        List<ActivitiDTO> activitiDTOS = activitiService.getActivitiHistoryList("案件流程");
        activitiDTOS.forEach(activitiDTO -> {
            System.out.println("流程ID:" + activitiDTO.getProcessId());
            System.out.println("流程名称:" + activitiDTO.getProcessName());
            System.out.println("流程的创建时间:" + activitiDTO.getStartTime());
            System.out.println("流程的结束时间:" + activitiDTO.getEndTime());
        });
    }

    //中间一条线
    @Test
    public void test2() throws Exception {
        //中间一条线
        String userName = "国土张三";
        String processId = activitiService.startProcess("案件流程", map);
        getProcessImg(processId);
        sp(userName,"0",processId);
        getProcessImg(processId);
        sp(userName,"1",processId);
        getProcessImg(processId);
        sp(userName,"0",processId);
        getProcessImg(processId);
        userName = "国土大队长";
        sp(userName,"0",processId);
        getProcessImg(processId);
        userName = "国土副支队长";
        sp(userName,"2",processId);
        getProcessImg(processId);
        userName = "监察李四";
        sp(userName,"0",processId);
        getProcessImg(processId);
        userName = "监察负责人";
        sp(userName,"0",processId);
        getProcessImg(processId);
        userName = "分管领导";
        sp(userName,"0",processId);
        getProcessImg(processId);
        userName = "分管王五";
        sp(userName,"0",processId);
        getProcessImg(processId);
        List<ActivitiDTO> activitiDTOS = activitiService.getActivitiHistoryList("案件流程");
        activitiDTOS.forEach(activitiDTO -> {
            System.out.println("流程ID:" + activitiDTO.getProcessId());
            System.out.println("流程名称:" + activitiDTO.getProcessName());
            System.out.println("流程的创建时间:" + activitiDTO.getStartTime());
            System.out.println("流程的结束时间:" + activitiDTO.getEndTime());
        });
    }

    //最长一条线
    @Test
    public void test3() throws Exception {
        //最长一条线
        String userName = "国土张三";
        String processId = activitiService.startProcess("案件流程", map);
        getProcessImg(processId);
        sp(userName,"0",processId);
        getProcessImg(processId);
        sp(userName,"1",processId);
        getProcessImg(processId);
        sp(userName,"0",processId);
        getProcessImg(processId);
        userName = "国土大队长";
        sp(userName,"0",processId);
        getProcessImg(processId);
        userName = "国土副支队长";
        sp(userName,"1",processId);
        getProcessImg(processId);
        userName = "国土支队长";
        sp(userName,"0",processId);
        getProcessImg(processId);
        userName = "监察李四";
        sp(userName,"0",processId);
        getProcessImg(processId);
        userName = "监察负责人";
        sp(userName,"0",processId);
        getProcessImg(processId);
        userName = "分管领导";
        sp(userName,"0",processId);
        getProcessImg(processId);
        userName = "分管王五";
        sp(userName,"0",processId);
        getProcessImg(processId);
        List<ActivitiDTO> activitiDTOS = activitiService.getActivitiHistoryList("案件流程");
        activitiDTOS.forEach(activitiDTO -> {
            System.out.println("流程ID:" + activitiDTO.getProcessId());
            System.out.println("流程名称:" + activitiDTO.getProcessName());
            System.out.println("流程的创建时间:" + activitiDTO.getStartTime());
            System.out.println("流程的结束时间:" + activitiDTO.getEndTime());
        });

    }

    @Test
    public void test4() throws IOException {
        activitiService.startProcess("急急急", map);
//        System.out.println(activitiService.getActivitiHistoryList("案件流程").toString());
//        System.out.println(activitiService.getActivitiHistoryInfo("11").toString());
//        System.out.println(activitiService.isFinished("11"));
    }

    //完成流程
    public void sp(String userName,String type,String processId){
        List<Task> taskList = activitiService.myTaskList(userName);
        taskList.forEach(task -> {
            Map<String, Object> vars = new HashMap<>();
            vars.put("remark", "通过");
            vars.put("name", userName);
            vars.put("type", type);
            List comments = new ArrayList();
            comments.add("进入下一流程");
            activitiService.myCompleteProcess(userName,processId,task.getId(),comments,vars);
        });
    }


    //把流转换为图片
    public void getProcessImg(String processId) throws IOException {
        byte[] data = activitiService.getProcessImg(processId);
        //new一个文件对象用来保存图片，默认保存当前工程根目录
        File imageFile = new File("E:/activitiImg/图片"+processId+"_"+System.currentTimeMillis()+".jpg");
        //创建输出流
        FileOutputStream fileOutStream = new FileOutputStream(imageFile);
        //写入数据
        fileOutStream.write(data);
    }

}
